<?php

namespace App\Console\Commands;

use App\Models\Car;
use App\Models\Category;
use App\Models\Config;
use App\Models\Credit;
use App\Models\Notification;
use App\Models\Order;
use App\Models\User;
use App\Services\CarService;
use App\Services\SmsService;
use Illuminate\Console\Command;

class SignOutCheck extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'sign_out_check';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }


    public function handle()
    {
        //检查是否未签到
        \DB::transaction(function () {
            $start = date("Y-m-d",strtotime("-1 day"));
            Car::where('status', Car::STATUS_SIGN_IN)->where('cur_date', '=', $start)->where('is_exec_sign_out', 0)->chunk(50, function ($list) {
                foreach ($list as $item) {
                    if ($item->appoint_user_id <0)
                        continue;
                    Credit::create([
                        'user_id' => $item->appoint_user_id,
                        'car_id' => $item->id,
                        'order_sn' => $item->order_sn,
                        'date' => $item->cur_date,
                        'type' => Credit::STATUS_NOT_OUT
                    ]);
                    $item->is_exec_sign_out = 1;
                    $item->is_punish = 1;
                    $item->save();

                    $user = User::find($item->appoint_user_id);
                    $user->not_sign_out_times = $user->not_sign_out_times + 1;
                    $user->save();
                    $cache_key = $item->getMorphClass() . ":" . $user->id;
                    \Cache::forget($cache_key);
                }
            });
        });

    }
}
